﻿using DoNet.Bpmn.Core.Vo;

namespace DoNet.Bpmn.Core.Utils
{
    public class SecurityUtils
    {
        // 获取当前登录用户的全部信息
        public static BaseIdTranStruVo? GetLogInEmpInfo()
        {
            var currentuser = ThreadLocalContainer.Get("currentuser");
            if (currentuser != null)
            {
                return (BaseIdTranStruVo)currentuser;
            }
            return default;
        }

        // 获取当前登录用户的 ID
        public static string GetLogInEmpId()
        {
            var currentuser = ThreadLocalContainer.Get("currentuser");
            if (currentuser != null)
            {
                var baseIdTranStru = (BaseIdTranStruVo)currentuser;
                if(baseIdTranStru != null)
                {
                    return baseIdTranStru.Id;
                }
            }
            throw new Exception("当前用户未登陆!");
        }

        // 获取当前登录用户的 ID (可以返回 null 或 "-999")
        public static string GetLogInEmpIdStr()
        {
            var currentuser = ThreadLocalContainer.Get("currentuser");
            if (currentuser != null)
            {
                var baseIdTranStru = (BaseIdTranStruVo)currentuser;
                if (baseIdTranStru != null)
                {
                    return baseIdTranStru.Id;
                }
            }
            throw new Exception("当前用户未登陆!");
        }

        // 获取当前登录用户的姓名
        public static string GetLogInEmpName()
        {
            var currentuser = ThreadLocalContainer.Get("currentuser");
            if (currentuser != null)
            {
                var baseIdTranStru = (BaseIdTranStruVo)currentuser;
                if (baseIdTranStru != null)
                {
                    return baseIdTranStru.Name;
                }
            }
            throw new Exception("当前用户未登陆!");
        }

        // 安全地获取当前登录用户的姓名，如果未登录则返回空字符串
        public static string GetLogInEmpNameSafe()
        {
            var currentuser = ThreadLocalContainer.Get("currentuser");
            if (currentuser != null)
            {
                var baseIdTranStru = (BaseIdTranStruVo)currentuser;
                if (baseIdTranStru != null)
                {
                    return baseIdTranStru.Name;
                }
            }
            return string.Empty;
        }

        // 安全地获取当前登录用户的 ID，如果未登录则返回 "-999"
        public static string GetLogInEmpIdSafe()
        {
            var currentuser = ThreadLocalContainer.Get("currentuser");
            if (currentuser != null)
            {
                var baseIdTranStru = (BaseIdTranStruVo)currentuser;
                if (baseIdTranStru != null)
                {
                    return baseIdTranStru.Id;
                }
            }
            return "-999";
        }
    }
}
